package cn.pn.project.service;

import cn.pn.project.common.RedisUtils;
import cn.pn.project.mapper.schedule.SeatcommodityDao;
import cn.pn.project.pojo.schedule.Seatcommodity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;

@RestController
public class SeatcommodityService {


    @Autowired
    private SeatcommodityDao seatcommodityDao;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据skuid查询座位信息
     * @param skuId
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/getCommodityBySkuId",method = RequestMethod.POST)
    public List<Seatcommodity> getCommodityBySkuId(@RequestParam("skuId")String skuId) throws SQLException{
        String key="SeatInfoBySkuId"+skuId;
        if(redisUtils.exist(key)){
            System.out.println("从redis中获取数据");
            return (List<Seatcommodity>)redisUtils.get(key);
        }else {
            if (redisUtils.set(key, seatcommodityDao.getCommodityBySkuId(skuId))) {
                System.out.println("把根据skuid查询座位信息放入到redis中");
            }
            return seatcommodityDao.getCommodityBySkuId(skuId);
        }

    }

}
